package cn.signalpha.modules.shop.controller.admin;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.dto.AuditDTO;
import cn.signalpha.common.dto.IdDTO;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.redis.cache.RedisCache;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.shop.entity.Adorn;
import cn.signalpha.modules.shop.service.AdornService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.time.Duration;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/admin/adorn")
@Api(value = "页面装修", tags = "页面装修")
public class AdornController extends BaseController {

	private final RedisCache redisCache;
	private final AdornService adornService;

    @GetMapping("/page")
    @ApiImplicitParams({
        @ApiImplicitParam(name="name", dataType = "string", value = "名称"),
        @ApiImplicitParam(name="isUse", dataType = "string", value = "是否使用"),
    })
    @ApiOperation(value = "分页", notes = "")
    public R<IPage<Adorn>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
        QueryWrapper<Adorn> wrapper = adornService.selectAdornWrapper(params);
        return R.data(adornService.page(Condition.getPage(query), wrapper));
    }

    @GetMapping("/list")
    @ApiOperation(value = "列表", notes = "")
    public R<List<Adorn>> list(@ApiIgnore @RequestParam Map<String, Object> params) {
        return R.data(adornService.list(adornService.selectAdornWrapper(params)));
    }

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<Adorn> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(adornService.getById(id));
	}

    @PostMapping("/submit")
    @ApiOperation(value = "提交", notes = "传入adorn")
    public R submit(@Valid @RequestBody Adorn adorn) {
		if(adorn.getId() == null){
			adorn.setCreateTime(new Date());
		}
		adorn.setUpdateTime(new Date());
		return R.status(adornService.saveOrUpdate(adorn));
    }

	@PostMapping("/update/use")
	@ApiOperation(value = "设为首页", notes = "")
	public R update(@Valid @RequestBody IdDTO idDTO) {
		adornService.update(Wrappers.<Adorn>lambdaUpdate().set(Adorn::getIsUse, 0));
		adornService.updateById(Adorn.builder().id(Long.valueOf(idDTO.getId())).isUse(1).build());
		return R.status(true);
	}

	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(adornService.removeByIds(Func.toLongList(ids)));
	}

	@PostMapping("/set/data")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "预览页面参数", notes = "")
	public R wechatShareCodeSetKey(@Valid AuditDTO auditDTO) {
		redisCache.setEx(auditDTO.getIds(), auditDTO.getRemark(), Duration.ofDays(1));
		return R.status(true);
	}

}
